/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.BorrowDocumentController;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import model.Base;
import model.Document;
import model.Observable;
import model.Observer;
import model.Person;
import model.User;

/**
 *
 * @author Ira
 */
public class BorrowDocumentView extends JPanel implements Observer, Observable {
	
	//<editor-fold defaultstate="collapsed" desc="noms des composants">
	public static final String TAB_BORROW = "tab_borrow";
	public static final String TAB_RESERVE = "tab_reserve";
	//</editor-fold>
	
	private Base model;
	private JTable tabDocs;
	private JTable tabReserve;
	private Document[] docs = null;
	private Document[] docsReserve = null;
	private BorrowDocumentController controller;
	private CalendarCellRenderer borrowDateRenderer = new CalendarCellRenderer();
	private ColorCalendarCellRenderer bringBackDateRenderer = new ColorCalendarCellRenderer();
	private DispoCellRenderer reserveRenderer = new DispoCellRenderer();
	private List<Observer> obs = new ArrayList<Observer>();
	
	private JPanel pnlBorrow = new JPanel(new BorderLayout());
	private JPanel pnlReserve = new JPanel(new BorderLayout());
	
	public BorrowDocumentView(Base model) {
		this.model = model;
		this.controller = new BorrowDocumentController(this);
		
		TitledBorder border = BorderFactory.createTitledBorder("Mes emprunts et réservations");
		this.setBorder(border);
		
		this.setLayout(new GridLayout(2, 1));
		this.setMinimumSize(new Dimension(290, 180));
		this.setPreferredSize(new Dimension(290, 180));
		this.setMaximumSize(new Dimension(290, 180));
		
		this.tabDocs = new JTable(new TableBorrowDocument(this.docs));
		this.tabDocs.getColumnModel().getColumn(2).setCellRenderer(this.borrowDateRenderer);
		this.tabDocs.getColumnModel().getColumn(3).setCellRenderer(this.bringBackDateRenderer);
		this.tabDocs.addMouseListener(this.controller);
		
		this.pnlBorrow.add(this.tabDocs.getTableHeader(), BorderLayout.NORTH);
		this.pnlBorrow.add(this.tabDocs, BorderLayout.CENTER);
		
		this.tabReserve = new JTable(new TableReserveDocument(this.docsReserve));
		this.tabReserve.setDefaultRenderer(GregorianCalendar.class, this.reserveRenderer);
		this.tabReserve.addMouseListener(this.controller);
		
		this.pnlReserve.add(this.tabReserve.getTableHeader(), BorderLayout.NORTH);
		this.pnlReserve.add(this.tabReserve, BorderLayout.CENTER);
		
		this.tabDocs.setName(BorrowDocumentView.TAB_BORROW);
		this.tabReserve.setName(BorrowDocumentView.TAB_RESERVE);
		
		this.add(this.pnlBorrow);
		this.add(this.pnlReserve);
	}
	
	public void majResult(User user, Document[] docs, Document[] reserve) {
		this.docs = docs;
		this.docsReserve = reserve;
		this.pnlBorrow.remove(this.tabDocs.getTableHeader());
		this.pnlBorrow.remove(this.tabDocs);
		this.tabDocs = new JTable(new TableBorrowDocument(docs));
		this.tabDocs.getColumnModel().getColumn(2).setCellRenderer(this.borrowDateRenderer);
		this.tabDocs.getColumnModel().getColumn(3).setCellRenderer(this.bringBackDateRenderer);
		this.tabDocs.setName(BorrowDocumentView.TAB_BORROW);
		this.tabDocs.addMouseListener(this.controller);
		this.pnlBorrow.add(this.tabDocs.getTableHeader(), BorderLayout.NORTH);
		this.pnlBorrow.add(this.tabDocs, BorderLayout.CENTER);
		
		this.pnlReserve.remove(this.tabReserve.getTableHeader());
		this.pnlReserve.remove(this.tabReserve);
		this.tabReserve = new JTable(new TableReserveDocument(reserve, user));
		this.tabReserve.setDefaultRenderer(GregorianCalendar.class, this.reserveRenderer);
		this.tabReserve.setName(BorrowDocumentView.TAB_RESERVE);
		this.tabReserve.addMouseListener(this.controller);
		this.pnlReserve.add(this.tabReserve.getTableHeader(), BorderLayout.NORTH);
		this.pnlReserve.add(this.tabReserve, BorderLayout.CENTER);
		this.validate();
		this.repaint();
	}

	@Override
	public void update(Object items) {
		if(items == null) {
			return;
		}
		try {
			List<Object> data = (List<Object>)items;
			User u = (User)data.get(0);
			Document[] docs = ((List<Document>)data.get(1)).toArray(new Document[0]);
			Document[] docsReserve = ((List<Document>)data.get(2)).toArray(new Document[0]);
			this.majResult(u, docs, docsReserve);
		}
		catch(Exception e) {
			try {
				this.model.shareDataWithObs((Person)items);
			}
			catch(ClassCastException e2) {
			
			}
		}
	}

	@Override
	public void addObserver(Observer obs) {
		this.obs.add(obs);
	}

	@Override
	public void updateObservers(Object data) {
		Boolean b = (Boolean)data;
		if(b) {
			if(this.tabDocs.getSelectedRow() != -1) {
				for(Observer obs: this.obs) {
					obs.update(this.docs[this.tabDocs.getSelectedRow()]);
				}
			}
		}
		else {
			if(this.tabReserve.getSelectedRow() != -1) {
				for(Observer obs: this.obs) {
					obs.update(this.docsReserve[this.tabReserve.getSelectedRow()]);
				}
			}
		}
	}

	@Override
	public void delObserver(Observer obs) {
		this.obs.remove(obs);
	}
}
